# -*- coding: utf-8 -*-
import os

# from IPython.external.qt_for_kernel import QtCore
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QMainWindow, QWidget, QFileDialog, QMessageBox

from src.controller.main_controller import MainController
from src.form.form_launch import Ui_launch
from src.utils import set_window_size_by_resolution, show_hide_layout, resource_path


class LaunchController(QWidget, Ui_launch):

    def __init__(self, parent=None):
        super(LaunchController, self).__init__(parent)
        self.setupUi(self)
        set_window_size_by_resolution(self, geometry_ratio=0.35)
        self.main_controller = None
        self.pushButton_compare_icon.hide()
        self.pushButton_compare.hide()
        self.pushButton_design.clicked.connect(self.show_main)
        self.comboBox.addItems(['《精轧螺纹钢筋预应力抗浮锚杆技术规程》（T/CECS 1706—2024）', '其他技术规范'])
        self.pushButton_exit.clicked.connect(self.close)

        self.icon_btns = [self.pushButton_rule, self.pushButton_workspace, self.pushButton_design_icon,
                          self.pushButton_compare_icon, self.pushButton_compare, self.pushButton_exit_icon]
        for icon_btn in self.icon_btns:
            icon_btn.setEnabled(False)

        self.pushButton_work_space_label.clicked.connect(self.choice_workspace)
        self.work_space_msg.setText(os.path.curdir)
        show_hide_layout(self.horizontalLayout_4,show=False)
        self.folder_path = os.path.curdir


    def show_main(self):
        self.hide()
        self.main_controller = MainController(self)
        self.main_controller.show()
        self.main_controller.pushButton_laya_kuoda.click()

    def choice_workspace(self):
        folder_path = QFileDialog.getExistingDirectory(
            self,
            "选择文件夹",
            os.path.expanduser("~"),  # 默认从用户目录开始
            QFileDialog.ShowDirsOnly
        )

        if not folder_path:  # 用户取消选择
            return
        print(folder_path)
        self.folder_path = folder_path
        self.work_space_msg.setText(folder_path)
        self.label_plan_path.setText(folder_path)
        # show_hide_layout(self.horizontalLayout_4,show=True)
    def closeEvent(self, event):
        reply = QMessageBox.question(
            self,
            "确认退出",
            "确定要退出程序吗？",
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No
        )

        if reply == QMessageBox.Yes:
            event.accept()  # 接受关闭事件
        else:
            event.ignore()  # 忽略关闭事件